package com.example.demo.order.service.impl;

import cn.com.antcloud.api.AntFinTechApiClient;
import cn.com.antcloud.api.AntFinTechProfile;
import cn.com.antcloud.api.twc.v1_0_0.request.CreateLeaseBizRequest;
import cn.com.antcloud.api.twc.v1_0_0.request.CreateLeaseRentalRequest;
import cn.com.antcloud.api.twc.v1_0_0.response.CreateLeaseBizResponse;
import cn.com.antcloud.api.twc.v1_0_0.response.CreateLeaseRentalResponse;
import cn.hutool.core.util.NumberUtil;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.demo.order.entity.Demo;
import com.example.demo.order.entity.Stages;
import com.example.demo.order.service.IStagesService;
import com.example.demo.order.service.IZlb;
import com.google.gson.Gson;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.UUID;

@Slf4j
@Service
public class ZlbImpl implements IZlb {
    @Autowired
    private IStagesService stagesService;

    @Autowired
    private AntFinTechApiClient client;


    @Override
    public boolean uploadGoodsInfo(String orderId) {
        return false;
    }

    @Override
    public boolean uploadUserInfo(String orderId) {
        return false;
    }

    @Override
    public boolean uploadOrderInfo(String orderId) {
        return false;
    }

    @Override
    public boolean uploadOrderItemsInfo(String orderId) {
        return false;
    }

    @Override
    public boolean uploadOrderExpressInfo(String orderId) {
        return false;
    }

    @Override
    public boolean uploadOrderPromiseInfo(String orderId) {
        return false;
    }

    @Override
    public boolean uploadOrderRentalInfo(String orderId) {
        LambdaQueryWrapper<Stages> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Stages::getFqStatus, 3);
        queryWrapper.eq(Stages::getZtwStatus, 1);
        queryWrapper.eq(Stages::getHasSync, false);
        queryWrapper.apply("now()>timestampadd(day,7,repayment_time)");
        List<Stages> result = stagesService.list(queryWrapper);

        result.forEach(stage -> {
            Demo demo = new Demo();
            demo.setOrderId(stage.getOrderId());
            demo.setCharge(NumberUtil.round(stage.getMonthlyRent() * 0.006, 2).multiply(new BigDecimal(10000)).longValueExact());
            demo.setRentalReturnState(3);
            demo.setRentalMoney(NumberUtil.round(stage.getMonthlyRent(), 2).multiply(new BigDecimal(10000)).longValueExact());
            demo.setReturnVoucherType(3);
            demo.setRemainTerm(Long.valueOf(stage.getTenancyTerm()) - stage.getCurrentTenancyTerm());
            demo.setLeaseTermIndex(stage.getCurrentTenancyTerm());
            demo.setIsFinish(false);
            demo.setRemainRental(NumberUtil.round(stage.getMonthlyRent(), 2).multiply(new BigDecimal(10000)).multiply(new BigDecimal(Long.valueOf(stage.getTenancyTerm()) - stage.getCurrentTenancyTerm())).longValueExact());
            demo.setReturnTime(stage.getRepaymentTime());
            demo.setReturnWay(4);
            demo.setReturnVoucherSerial(UUID.randomUUID().toString());
            CreateLeaseBizRequest createLeaseBizRequest = new CreateLeaseBizRequest();
            createLeaseBizRequest.setProductInstanceId("notary-api-prod");
            createLeaseBizRequest.setRegionName("CN-HANGZHOU-FINANCE");
            createLeaseBizRequest.setBizContent(new Gson().toJson(demo));
            createLeaseBizRequest.setType("rental");
            try {
                log.info("租赁宝入参：" + new Gson().toJson(createLeaseBizRequest));
                CreateLeaseBizResponse response = client.execute(createLeaseBizRequest);
                log.info("租赁宝结果：" + new Gson().toJson(response));
                stage.setHasSync(true);
                UpdateWrapper<Stages> updateWrapper = new UpdateWrapper<>();
                updateWrapper.eq("uuid", stage.getUuid()).set("has_sync", true);
                stagesService.update(updateWrapper);
            } catch (Exception e) {
                e.printStackTrace();
                log.error(e.getMessage());
            }


        });

        return false;
    }

    @Override
    public boolean uploadOrderQueryInfo(String orderId) {
        return false;
    }
}
